package com.fastjrun.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.filter.GenericFilterBean;

import com.fastjrun.common.MediaTypes;
import com.fastjrun.helper.BaseResponseHelper;
import com.fastjrun.packet.BaseDefaultResponseBody;
import com.fastjrun.packet.BaseResponse;

public class ForbiddenFilter extends GenericFilterBean {

    private String[] forbiddenMatchers;

    public void setForbiddenMatchers(String forbiddenMatchers) {
        this.forbiddenMatchers = forbiddenMatchers.split(",");
    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        String yourIP = getIpAddr(request);
        logger.info(yourIP + ":request.getServletPath()="
                + request.getServletPath());
        logger.info("request.getUA=" + request.getHeader("user-agent"));
        if (forbiddenMatchers != null && forbiddenMatchers.length > 0) {
            for (String forbiddenMatcher : forbiddenMatchers) {
                logger.debug(forbiddenMatcher);
                if (request.getServletPath().matches(forbiddenMatcher)) {
                    HttpHeaders headers = new HttpHeaders();

                    String reqContentType = request.getHeader("Content-Type")
                            .replaceAll(" ", "");

                    headers.setContentType(MediaType
                            .parseMediaType(reqContentType));

                    if (MediaTypes.JSON_UTF_8.equals(reqContentType)) {
                        BaseResponse<BaseDefaultResponseBody> result = BaseResponseHelper
                                .getFailResult("SYS0001", "当前请求不被支持");

                        JSONObject jsonObject = JSONObject.fromObject(result);

                        res.setContentType(reqContentType);

                        res.getWriter().print(jsonObject);
                        return;
                    }
                }
            }
        }
        chain.doFilter(req, res);
    }

    public void destroy() {

    }

    private static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
